/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo4;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.*;
import java.io.PrintWriter;
import Modelo.DAL.ConexionBD;
import Modelo.DAL.Permisos_DAO;
import java.sql.*;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author dayana
 */
public class consultaPersonalRepososPermisosAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        //Obtengo el usbid de la persona logueada
        HttpSession sesion = request.getSession();
        String user = (String) sesion.getAttribute("user");
        System.out.println("La persona que esta logueada es: " + user);

        String ci = Permisos_DAO.obtenerCedula2(user);

        System.out.println("Ci: " + ci);

        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String where = "";

        if (!limit.equals("0")) {
            where += " limit " + limit + " offset " + offset;
        }

        response.setContentType("text/json");
        JSONObject reposos = Permisos_DAO.listarAll(ci, where);
        JSONObject ext = new JSONObject();
        ext.put("VerReposos", reposos);

        PrintWriter pw = response.getWriter();
        pw.write(ext.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }

}
